<link rel="stylesheet" type="text/css" href="/assets/third-party/datatables/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="/assets/css/dataTables.bootstrap.css">
<link rel="stylesheet" type="text/css" href="/assets/third-party/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css">

<style type="text/css">
	.il{
		display: inline;
	}
	.form-group .control-label{
		padding-right: 5px;
	}
	.form-group .col-sm-7{
		padding-left: 5px;
	}
	.form-group input, .form-group select{
		width: 88%;
		display: inline;
	}
</style>

<div class="container" style="margin-top: 60px;">
	<div class="row">
		<div class="col-lg-12">
			<!--<a href="#" class="btn btn-info"> <i class="fa fa-search"></i> 查询 </a> -->
			<a class="btn btn-success" data-toggle="modal" data-target="#couponModal"> <i class="fa fa-plus"></i> 新增优惠券 </a>
			<!--<a href="#" class="btn btn-success"> <i class="fa fa-plus"></i> 全选 </a>
			<a href="#" class="btn btn-danger"> <i class="fa fa-plus"></i> 批量删除 </a>
			<a href="#" class="btn btn-danger"> <i class="fa fa-plus"></i> 刷新 </a>-->
		</div>
	</div>
	<div class="row" style="margin-top: 10px;">
		<div class="col-lg-12">
			<div class="panel panel-default">
                <div class="panel-heading">
                    优惠券列表
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
					<table id="coupons" class="table table-bordered table-hover">
						<thead>
							<tr>
								<th>ID</th>
								<th>优惠券标题</th>
								<th>金额</th>
								<th>数量</th>
								<th>已领取</th>
								<th>使用条件</th>
								<th>有效期</th>
								<!--<th>类型</th>-->
								<th>领取地址</th>
								<th>创建时间</th>
								<th style="width: 240px;">操作</th>
							</tr>
						</thead>
						<tbody id="couponList">
							<?php foreach ($items as $key => $value) { ?>
							<tr data-id="<?php echo $value->id; ?>">
								<td><?php echo $value->id; ?></td>
								<td><?php echo $value->title; ?></td>
								<td><?php echo $value->money; ?></td>
								<td><?php echo $value->count; ?></td>
								<td><?php echo count($value->records); ?></td>
								<td><?php echo $value->quota > 0 ? "满{$value->quota}使用" : "买就可以用"; ?></td>
								<td><?php echo $value->start == $value->end ? '永久有效' : date('Y-m-d H:i:s', $value->start) ."至<br>" . date('Y-m-d H:i:s', $value->end); ?></td>
								<!--<td><?php //echo \Model_Coupon::$_maps['type'][$value->type]; ?></td>-->
								<td><a href="<?php echo \Config::get('base_url') . "m/coupon/receive/{$value->id}"; ?>">领取地址</a></td>
								<td><?php echo date('Y-m-d H:i:s', $value->created_at); ?></td>
								<td>
									<a role="edit" class="btn btn-sm btn-info" data-toggle="modal" data-target="#couponModal"> <i class="fa fa-pencil-square-o"></i> 编辑 </a>
									<a href="#" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirmModal"> <i class="fa fa-trash-o"></i> 删除 </a>
									<div class="btn-group">
										<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-money"></i> 赠送优惠券 <span class="caret"></span></button>
										<ul class="dropdown-menu" role="menu">
											<li><a href="#" data-toggle="modal" data-target="#giveModal">按会员组发送</a></li>
											<li class="divider"></li>
											<li><a href="#" data-toggle="modal" data-target="#giveMemberModal">选择会员发送</a></li>					
										</ul>
									</div>
								</td>
							</tr>
							<?php } ?>							
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 发布优惠券模态框 -->
<div class="modal fade" id="giveModal" tabindex="-1" role="dialog" aria-labelledby="giveModalTitle" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="giveModalTitle"> 按会员级别赠送优惠券 </h4>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<label for="phone">会员级别</label>
					<select id="level_id" class="form-control">
						<?php foreach ($levels as $key => $value) { ?>
						<option value="<?php echo $value->id; ?>"><?php echo $value->name; ?></option>
						<?php } ?>
					</select>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"> 取消 </button>
				<button type="button" id="btnGrive" class="btn btn-primary"> 赠送 </button>
			</div>
		</div>
	</div>
</div>

<!-- 按会员发布优惠券模态框 -->
<div class="modal fade" id="giveMemberModal" tabindex="-1" role="dialog" aria-labelledby="giveMemberModalTitle" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="giveMemberModalTitle"> 按会员赠送优惠券 </h4>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<table id="memberList" class="table table-bordered table-hover">
						<thead>
							<tr>
								<th>会员卡号</th>
								<th>会员姓名</th>
								<th>手机号码</th>
								<th>会员级别</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ($members as $key => $value) { ?>
							<tr data-id="<?php echo $value->id; ?>" uid="<?php echo $value->user_id;?>">
								<td><?php echo $value->no; ?></td>
								<td><?php echo $value->people && $value->people->first_name ? $value->people->first_name . $value->people->last_name : '-'; ?></td>
								<td><?php echo $value->people && $value->people->phone ? $value->people->phone : '-'; ?></td>
								<td><?php echo $value->level && $value->level->name ? $value->level->name : '-'; ?></td>
								<td><a href="#" class="btn btn-sm btn-success" role="give"> 赠送 </a></td>
							</tr>
							<?php } ?>							
						</tbody>
					</table>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"> 关闭 </button>
			</div>
		</div>
	</div>
</div>

<!-- 优惠券详情模态框 -->
<div class="modal fade" id="couponModal" tabindex="-1" role="dialog" aria-labelledby="couponModalTitle" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="couponModalTitle"> 优惠券资料 </h4>
			</div>
			<div class="modal-body">
				<form id="frmCoupon" role="form" action="" method="post">
					<div class="form-group">
						<label for="phone">标&nbsp;&nbsp;&nbsp;&nbsp;题</label>
						<input type="text" class="form-control" id="title" name="title" placeholder="优惠券的标题" value="">
					</div>
					<!--<div class="form-group">
						<label for="price">类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型</label>
						<select id="level_id" name="level_id" class="form-control">
							<option value="">减免券</option>
							<option value="">折扣券</option>
						</select>
					</div>-->
					<div class="form-group">
						<label for="phone">金&nbsp;&nbsp;&nbsp;&nbsp;额</label>
						<input type="number" class="form-control" id="money" name="money" placeholder="优惠券的面值">
					</div>
					<div class="form-group">
						<label for="gender">数&nbsp;&nbsp;&nbsp;&nbsp;量</label>
						<input type="number" class="form-control" id="count" name="count" placeholder="发行数量">
					</div>
					<div class="form-group">
						<label for="phone">条&nbsp;&nbsp;&nbsp;&nbsp;件</label>
						<input type="number" class="form-control" id="quota" name="quota" placeholder="满足多少元才可以使用">
					</div>
					<div class="form-group">
						<label for="price">有效期</label>
						<input type="text" class="form-control" id="start" name="start" placeholder="开始时间" style="width: 44%; display: inline;" role="datetime">
						<input type="text" class="form-control" id="end" name="end" placeholder="结束时间" style="width: 44%; display: inline;" role="datetime">
					</div>
					<div class="form-group">
						<label for="price">地&nbsp;&nbsp;&nbsp;&nbsp;址</label>
						<input type="text" class="form-control" readonly="readonly" id="url" placeholder="保存后查看">
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"> 取消 </button>
				<button type="button" id="btnSave" class="btn btn-primary"> 保存 </button>
			</div>
		</div>
	</div>
</div>

<!-- 确认删除模态框 -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog" aria-labelledby="confirmModalTitle" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-body">
				<div class="alert alert-warning">
					<strong>确定删除？</strong>
					<p>删除该优惠券，所有会员领取到的优惠券将被清空!</p>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"> 取消 </button>
				<button id="btnConfirm" type="button" class="btn btn-danger"> 确定 </button>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript" src="/assets/third-party/datatables/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/assets/js/dataTables.bootstrap.js"></script>
<script type="text/javascript">
	var coupon_id = 0;
	var flag = false;

	$(function(){

		$('a[data-target="#giveModal"]').click(function(){
			coupon_id = $(this).parents('tr').attr('data-id');
		});

		$('a[data-target="#giveMemberModal"]').click(function(){
			coupon_id = $(this).parents('tr').attr('data-id');
		});

		$('.table').dataTable({
	        "oLanguage": {
	            "sLengthMenu": "每页显示 _MENU_ 条记录",
	            "sZeroRecords": "很抱歉，未找到相关数据",
	            "sInfo": "从 _START_ 至 _END_ /共 _TOTAL_ 条数据",
	            "sInfoEmpty": "没有符合条件的数据",
	            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
	            "sSearch": "搜索：",
	            "oPaginate": {  
					"sFirst": "首页",  
					"sPrevious": "上一页",
					"sNext": "下一页",
					"sLast": "末页"
				}  
	        },
	        fnDrawCallback: function(){

	            if(window.flag == false){
	        		window.flag = true;
	        		var index = localStorage.getItem('pagination_index');
	                localStorage.removeItem('pagination_index');
		        	if( ! index){
		        		index = 1;
		        	}
		        	index -= 1;

		        	$('#coupons').dataTable().fnPageChange(index);
	                
	        	}
            }
	    });

		$(document).delegate('a[data-target="#couponModal"]', 'click', function(){

	    	$("#title").val('');
	    	$('#money').val('');
	    	$('#count').val('');
	    	$('#quota').val('');
	    	$('#start').val('');
	    	$('#end').val('');
	    	$('#url').val("");

	    	if($(this).attr('role') == 'edit'){
	    		localStorage.setItem('pagination_index', $('.pagination .active').find('a').text());
	    		$('#frmCoupon').attr('action', '/admin/coupon/edit/' + $(this).parent().parent().attr('data-id'));
	    		$.get('/admin/coupon/view/' + $(this).parent().parent().attr('data-id'), 
	    			function(data, status){
	    				if(data.status == 'succ'){
	    					var item = data.data;
	    					$("#title").val(item.title);
	    					$('#money').val(item.money);
	    					$('#count').val(item.count);
	    					$('#quota').val(item.quota);
	    					$('#start').val(item.start);
	    					$('#end').val(item.end);
	    					$('#url').val(data.url + "m/coupon/receive/" + item.id);
	    				}
	    			}, 'json');
	    	}else{
	    		$('#frmCoupon').attr('action', '/admin/coupon/create/');
	    	}
	    });

		$('table').delegate('a[data-target="#confirmModal"]', 'click', function(){
	    	$('#btnConfirm').attr('data-id', $(this).parent().parent().attr('data-id'));
	    });

	    $('#btnConfirm').click(function(){
	    	var id = $(this).attr('data-id');
	    	$.get('/admin/coupon/delete/' + id,
	    		function(data, status){
	    			if(data.status == 'succ'){
	    				$('#couponList').find('tr[data-id="' + id + '"]').remove();
	    				$('#confirmModal').modal('hide');
	    			}
	    		}, 'json');
	    });

	    $('#btnGrive').click(function(){
	    	$.post('/admin/coupon/give/group', 
	    		{
	    			'coupon_id' : coupon_id,
	    			'group_id' : $('#level_id').val()
	    		},
	    		function(data, status){
	    			if(data.status == 'succ'){
	    				alert(data.msg);
	    			}else{
	    				alert(data.msg);
	    			}
	    		}, 'json');
	    });

	    $('#memberList').delegate('a[role="give"]', 'click', function(){
	    	var a = $(this);
	    	$.post('/admin/coupon/give', 
	    		{
	    			'coupon_id' : coupon_id,
	    			'user_id' : a.parents('tr').attr('uid')
	    		},
	    		function(data, status){
	    			if(data.status == 'succ'){
	    				var obj = a.parent();
	    				a.remove();
	    				obj.append("已赠送");
	    			}else{
	    				alert(data.msg);
	    			}
	    		}, 'json');
	    });
	});
</script>

<script type="text/javascript" src="/assets/third-party/parsley/parsley.min.js"></script>
<script type="text/javascript" src="/assets/third-party/parsley/i18n/zh_cn.js"></script>
<script type="text/javascript" src="/assets/third-party/parsley/i18n/zh_cn.extra.js"></script>
<script type="text/javascript">
	$(function(){
		$('#btnSave').click(function(){
			$('#frmCoupon').submit();
		});
	});
</script>

<script type="text/javascript" src="/assets/third-party/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="/assets/third-party/bootstrap-datetimepicker-master/js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>
<script type="text/javascript">
$(function(){
	$('input[role="datetime"]').datetimepicker({
		language: 'zh-CN'
	});
});
</script>